package cn.wolfcode.luowowo2.website.web.controller;

import cn.wolfcode.luowowo2.article.domain.*;
import cn.wolfcode.luowowo2.article.service.IDestinationService;
import cn.wolfcode.luowowo2.article.service.IRegionService;
import cn.wolfcode.luowowo2.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo2.article.service.ITravelService;
import cn.wolfcode.luowowo2.cache.domain.TravelComment;
import cn.wolfcode.luowowo2.cache.service.ITravelCommentService;
import cn.wolfcode.luowowo2.common.query.TravelCommentQueryObject;
import cn.wolfcode.luowowo2.common.query.TravelQueryObject;
import cn.wolfcode.luowowo2.common.util.AjaxResult;
import cn.wolfcode.luowowo2.member.domain.UserInfo;
import cn.wolfcode.luowowo2.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo2.website.web.annotation.UserParam;
import cn.wolfcode.luowowo2.website.web.util.UMEditorUploader;
import cn.wolfcode.luowowo2.website.web.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by Albert on 2019/7/11.
 */
@Controller
@RequestMapping("travel")
public class TravelController {
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelCommentService travelCommentService;


    @RequestMapping("")
    public String travel(Model model, @ModelAttribute("qo") TravelQueryObject qo){
        PageInfo<Travel> pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "travel/list";
    }

    @RequestMapping("detail")
    public String detail(Model model, Long id, @ModelAttribute("qo") TravelCommentQueryObject qo){
        Travel travelDetail = travelService.get(id);

        model.addAttribute("detail",travelDetail);

        List<Destination> toastList = destinationService.getToastList(259L);
        model.addAttribute("toasts",toastList);

        List<StrategyDetail> sds = strategyDetailService.getByViewNumTop3(travelDetail.getDest().getId());
        model.addAttribute("sds",sds);

        List<Travel> ts = travelService.getByViewNumTop3(travelDetail.getDest().getId());
        model.addAttribute("ts",ts);

        qo.setTravelId(id);
        Page<TravelComment> list = travelCommentService.query(qo);
        model.addAttribute("list",list.getContent());

        return "travel/detail";
    }
    @RequireLogin
    @RequestMapping("input")
    public String input(Model model,Long id){
        if (id!=null) {
            Travel travel = travelService.get(id);
            model.addAttribute("tv",travel);
        }

        List<Destination> destinations = destinationService.getDestByDeep(3);
        model.addAttribute("destinations",destinations);
        return "travel/input";
    }

    @Value("${file.path}")
    private String filePath;

    @RequestMapping("coverImageUpload")
    @ResponseBody
    public String uploadImg(MultipartFile pic){
        String fileName = UploadUtil.upload(pic, filePath);
        return fileName;
    }

    //上传图片
    @RequireLogin
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile,HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, filePath);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }
    @RequireLogin
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Travel travel, @UserParam UserInfo userInfo){
        travel.setAuthor(userInfo);
        Long id = travelService.saveOrUpdate(travel);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setData(id);
        return ajaxResult;
    }

    @RequireLogin
    @RequestMapping("commentAdd")
    public String commentAdd(Model model,TravelComment comment,int floor,@UserParam UserInfo userInfo){
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setCreateTime(new Date());
        TravelComment travelComment = travelCommentService.save(comment);
        model.addAttribute("c",travelComment);
        model.addAttribute("floor",floor + 1);
        return "travel/commentTpl";
    }

}
